package com.irdstudio.efp.flow.api.rest;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.efp.flow.service.facade.BizFlowParamService;
import com.irdstudio.efp.flow.service.vo.BizFlowParamVO;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.core.log.LogManager;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class BizFlowParamController extends AbstractController  {

	@Autowired
	@Qualifier("bizFlowParamService")
	private BizFlowParamService bizFlowParamService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/biz/flow/params", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizFlowParamVO>> queryBizFlowParamAll(
			@RequestBody BizFlowParamVO vo) {		

		LogManager.info(vo.getBizParamCode(), bizFlowParamService, "queryAll");
		
		//List<BizFlowParamVO> outputVo = this.executeQueryList("queryAll", bizFlowParamService, new Object[]{vo});

		List<BizFlowParamVO> outputVo = null;

		// 根据用户角色获取用户权限方案
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		switch (dataRuleType) {
		case Owner:
			outputVo = bizFlowParamService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = bizFlowParamService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = bizFlowParamService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = bizFlowParamService.queryAllOwner(vo);
			break;
		}

		return getResponseData(outputVo);

	}

	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/biz/flow/param/{bizParamCode}{bizFlowId}/{bizParamIo}/", method=RequestMethod.GET)
	public @ResponseBody ResponseData<BizFlowParamVO> queryByPk(@PathVariable("bizParamCode") String bizParamCode,@PathVariable("bizFlowId") String bizFlowId,@PathVariable("bizParamIo") String bizParamIo) {		
		BizFlowParamVO inVo = new BizFlowParamVO();
		inVo.setBizParamCode(bizParamCode);
		inVo.setBizFlowId(bizFlowId);
		inVo.setBizParamIo(bizParamIo);
		BizFlowParamVO outputVo = bizFlowParamService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * @param bizFlowParam
	 * @return
	 */
	@RequestMapping(value="/biz/flow/param", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody BizFlowParamVO inBizFlowParamVo) {		
		int outputVo = bizFlowParamService.deleteByPk(inBizFlowParamVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * @param inBizFlowParamVo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/param", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody BizFlowParamVO inBizFlowParamVo) {		
		int outputVo = bizFlowParamService.updateByPk(inBizFlowParamVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * @param inBizFlowParamVo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/param", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertBizFlowParam(@RequestBody BizFlowParamVO inBizFlowParamVo) {
		int outputVo = bizFlowParamService.insertBizFlowParam(inBizFlowParamVo);
		return getResponseData(outputVo);

	}
}
